package com.jackchen.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.jackchen.dao.UserDAO;
import com.jackchen.po.User;

/**
 * A data access object (DAO) providing persistence and search support for
 * User entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see
 * @author MyEclipse Persistence Tools
 */

public class UserDAOImpl extends IBaseDAOImpl implements UserDAO{
	
	private static final Logger log = Logger.getLogger(UserDAOImpl.class);
	
	
	public User findByUserNameAndPassword(String userName,String password) {
		log.debug("finding User instance with user name:"+userName+" and password:"+password);
		try{
			String queryString = "from User as model where model.username =:userName and model.password =:password";
			String[] queryParamName = new String[2];
			queryParamName[0] = "userName";
			queryParamName[1] = "password";
			Object[] queryValues = new Object[2];
			queryValues[0] = userName;
			queryValues[1] = password;
			List<User> list = findByNamedParam(queryString,queryParamName,queryValues);
			if(list!=null&&list.size()>0){
				return list.get(0);
			}else{
				return null;
			}
		}catch(RuntimeException re){
			log.error("find by property name failed", re);
			throw re;
		}
	}

	@Override
	public User findByUserName(String userName) {
		log.debug("finding User instance with user name:"+userName);
		String queryString = "from User as model where model.username='" + userName + "'";
		List list = findObjByHSql(queryString);
		if (list != null && list.size() == 1) {
			return (User) list.get(0);
		}
		return null;
	}

	@Override
	public int deleteUsers(String[] userIds) {
		final StringBuffer hsql = new StringBuffer("delete from userinfo where userId in (");
		for (String userId : userIds) {
			hsql.append("'");
			hsql.append(userId);
			hsql.append("',");
		}
		hsql.deleteCharAt(hsql.length() - 1);
		hsql.append(")");
		
		int result = this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createSQLQuery(hsql.toString());
				int rows = query.executeUpdate();
				return rows;
			}
		});
		return result;
	}

	@Override
	public Map findUserOnPage(String keyword, String regionGroupId,
			String userGroupId, String onlineState, int pageSize, int startRow) {
		String baseSql = " from User u where 1=1";
		StringBuffer hsql = new StringBuffer(baseSql);
		// 添加关键字查询条件
		if (keyword != null && !"".equals(keyword.trim())) {
			// 模糊匹配用户的"用户名", "真实姓名", "电话号码"和"邮箱地址"
			hsql.append(" AND (u.userName like '%" + keyword + "%'");
			hsql.append(" OR");
			hsql.append(" u.realName like '%" + keyword + "%'");
			hsql.append(" OR");
			hsql.append(" u.phone like '%" + keyword + "%'");
			hsql.append(" OR");
			hsql.append(" u.email like '%" + keyword + "%'");
			hsql.append(")");
		}
		// 添加管理域组过滤条件
		if (regionGroupId != null && !"-1".equals(regionGroupId)) {
			hsql.append(" AND u.regiongroup.regionGroupId=" + regionGroupId);
		}
		// 添加用户组过滤条件
		if (userGroupId != null && !"-1".equals(userGroupId)) {
			hsql.append(" AND u.usergroup.userGroupId=" + userGroupId);
		}
		// 添加在线状态过滤条件
		if (onlineState != null && !"-1".equals(onlineState)) {
			Integer online = Integer.valueOf(onlineState);
			hsql.append(" AND u.online=" + online);
		}
		
		return findObjBySqlPage(hsql.toString(), pageSize, startRow);
	}
	
}